﻿using BW.MLTT.Domain.DTO;
using BW.MLTT.Domain.Entity;
using BW.MLTT.IRepository;
using BW.MLTT.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.MLTT.Service
{
    public class GoodService : BaseService<T_Good>, IGoodService
    {
        private readonly ICultivationEnterpriseRepository _CultivationEnterpriseRepositoryrepository;
        private readonly ICultivationRepository _CultivationRepositoryrepository;
        private readonly IGoodRepository _GoodRepositoryrepository;
        private readonly IGoodImmuneRepository _GoodImmuneRepositoryrepository;
        private readonly IImagesRepository _ImagesRepositoryrepository;
        private readonly IImmuneRepository _ImmuneRepositoryrepository;
        private readonly IProcessEnterpriseRepository _ProcessEnterpriseRepositoryrepository;
        private readonly IProcessRepository _ProcessRepositoryrepository;
        private readonly ISlaughterEnterpriseRepository _SlaughterEnterpriseRepositoryrepository;
        private readonly ISlaughterRepository _SlaughterRepositoryrepository;
        private readonly ISupplierRepository _SupplierRepositoryrepository;
        private readonly IUserRepository _UserRepositoryrepository;
        public GoodService(IGoodRepository GoodRepositoryrepository, ICultivationEnterpriseRepository cultivationEnterpriseRepositoryrepository, ICultivationRepository cultivationRepositoryrepository, IGoodImmuneRepository goodImmuneRepositoryrepository, IImagesRepository imagesRepositoryrepository, IImmuneRepository immuneRepositoryrepository, IProcessEnterpriseRepository processEnterpriseRepositoryrepository, IProcessRepository processRepositoryrepository, ISlaughterEnterpriseRepository slaughterEnterpriseRepositoryrepository, ISlaughterRepository slaughterRepositoryrepository, ISupplierRepository supplierRepositoryrepository, IUserRepository userRepositoryrepository) : base(GoodRepositoryrepository)
        {
            _CultivationEnterpriseRepositoryrepository = cultivationEnterpriseRepositoryrepository;
            _CultivationRepositoryrepository = cultivationRepositoryrepository;
            _GoodRepositoryrepository = GoodRepositoryrepository;
            _GoodImmuneRepositoryrepository = goodImmuneRepositoryrepository;
            _ImagesRepositoryrepository = imagesRepositoryrepository;
            _ImmuneRepositoryrepository = immuneRepositoryrepository;
            _ProcessEnterpriseRepositoryrepository = processEnterpriseRepositoryrepository;
            _ProcessRepositoryrepository = processRepositoryrepository;
            _SlaughterEnterpriseRepositoryrepository = slaughterEnterpriseRepositoryrepository;
            _SlaughterRepositoryrepository = slaughterRepositoryrepository;
            _SupplierRepositoryrepository = supplierRepositoryrepository;
            _UserRepositoryrepository = userRepositoryrepository;
        }
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="listcount"></param>
        /// <param name="pagecount"></param>
        /// <returns></returns>

        public List<T_Good> GetGoods(int pageindex, int pagesize, out int listcount, out int pagecount)
        {
            var list = _GoodRepositoryrepository.GetAll().Where(p => p.IsDeleted == false);
            listcount = list.Count();
            pagecount = (int)Math.Ceiling(listcount * 1.0 / pagesize);
            list=list.OrderBy(p=>p.GoodId).Skip((pageindex-1)*pagesize).Take(pagesize);
            return list.ToList();

        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="loginDTO"></param>
        /// <returns></returns>
        public T_User Login(RequestLoginDTO loginDTO)
        {
            var list=_UserRepositoryrepository.GetAll().FirstOrDefault(p=>p.UserName==loginDTO.UserName && p.Password==loginDTO.Password);
            return list;
        }
        #region 商品
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        public int AddGoods(T_Good good)
        {
            _GoodRepositoryrepository.Add(good);
            return SaveChange();
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public  int DeletedGood(List<T_Good> goods)
        {
            goods.ForEach(c =>
            {
                c.IsDeleted = true;
            });
            _GoodRepositoryrepository.UpdateRange(goods);
            return SaveChange();
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        public int UpdateGood(T_Good good)
        {
            _GoodRepositoryrepository.Update(good);
            return SaveChange();
        }
        /// <summary>
        /// 批量添加商品
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        public  int AddRangeGood(List<T_Good> goods)
        {
            _GoodRepositoryrepository.AddRange(goods);
            return SaveChange();
        }
        /// <summary>
        /// 添加图片到图片表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Url"></param>
        /// <returns></returns>
        public int AddImage(string Id, string Url)
        {
            T_Images images = new T_Images()
            {
                ImageAddress=Url,
                ImageFkId=Id,
            };
            _ImagesRepositoryrepository.Add(images);
            return SaveChange();
        }
        /// <summary>
        /// 删除图片表的图片
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public int DeletedImages(string Url)
        {
            var list= _ImagesRepositoryrepository.GetAll().FirstOrDefault(c=>c.ImageAddress==Url);
            _ImagesRepositoryrepository.Delete(list);
            return SaveChange();
        }
        /// <summary>
        /// 获取图片反填
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<T_Images> GetT_Images(string ID)
        {
            var list = _ImagesRepositoryrepository.GetAll().Where(p => p.ImageFkId == ID);
            return list.ToList();
        }

        #endregion
        #region 养殖
        /// <summary>
        /// 养殖信息表添加
        /// </summary>
        /// <param name="cultivation"></param>
        /// <returns></returns>
        public int AddCultivation(T_Cultivation cultivation)
        {
            UpdateGoodPerfect(cultivation.GoodId);
            _CultivationRepositoryrepository.Add(cultivation);
            
            return SaveChange();
        }
        /// <summary>
        /// 养殖信息表修改
        /// </summary>
        /// <param name="cultivation"></param>
        /// <returns></returns>
        public int UpdateCultivation(T_Cultivation cultivation)
        {
            _CultivationRepositoryrepository.Update(cultivation);
            return SaveChange();
        }
        /// <summary>
        /// 养殖信息表反填
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_Cultivation GetCultivationFind(string goodId)
        {
            var list = _CultivationRepositoryrepository.GetAll().FirstOrDefault(p => p.GoodId == goodId);
            return list;
        }
        /// <summary>
        /// 显示保健信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public List<T_Immune> GetT_Immunes(string goodId)
        {
            var list = _ImmuneRepositoryrepository.GetAll().Where(p => p.GoodId == goodId);
            return list.ToList();

        }
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        public int AddImmunes(T_Immune immune)
        {
            
            var list= _ImmuneRepositoryrepository.GetAll().FirstOrDefault(p=>p.GoodId == immune.GoodId);
            if(list==null)
            {
                UpdateGoodPerfect(immune.GoodId);
            }
            _ImmuneRepositoryrepository.Add(immune);
            return SaveChange();
        }
        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        public int UpdateImmunes(T_Immune immune)
        {
            _ImmuneRepositoryrepository.Update(immune);
            return SaveChange();
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="immune"></param>
        /// <returns></returns>
        public int DeletedImmunes(T_Immune immune)
        {
            _ImmuneRepositoryrepository.Delete(immune);
            var list = _ImmuneRepositoryrepository.GetAll().Where(p => p.GoodId == immune.GoodId).Count();
            if (list == 0)
            {
                var list1 = _GoodRepositoryrepository.GetAll().FirstOrDefault(p => p.GoodId == immune.GoodId);
                list1.GoodPerfect -= 1;
                _GoodRepositoryrepository.Update(list1);
            }
            return SaveChange();
        }
        /// <summary>
        /// 添加养殖企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public int AddCultivationEnterprise(T_CultivationEnterprise enterprise)
        {
            UpdateGoodPerfect(enterprise.GoodId);
            _CultivationEnterpriseRepositoryrepository.Add(enterprise);
            return SaveChange();
        }
        /// <summary>
        /// 修改养殖企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public int UpdateCultivationEnterprise(T_CultivationEnterprise enterprise)
        {
            _CultivationEnterpriseRepositoryrepository.Update(enterprise);
            return SaveChange();
        }
        /// <summary>
        /// 养殖企业反填
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_CultivationEnterprise GetCultivationEnterpriseFind(string goodId)
        {
          var list=  _CultivationEnterpriseRepositoryrepository.GetAll().FirstOrDefault(c=>c.GoodId == goodId);
            return list;
        }

        #endregion
        #region 屠宰
        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="slaughter"></param>
        /// <returns></returns>
        public int AddSlaughter(T_Slaughter slaughter)
        {
            UpdateGoodPerfect(slaughter.GoodId);
            _SlaughterRepositoryrepository.Add(slaughter);
            return SaveChange();
        }
        /// <summary>
        /// 修改屠宰信息
        /// </summary>
        /// <param name="slaughter"></param>
        /// <returns></returns>
        public int UpdateSlaughter(T_Slaughter slaughter)
        {
            _SlaughterRepositoryrepository.Update(slaughter);
            return SaveChange();
        }
        /// <summary>
        /// 反填屠宰信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_Slaughter GetSlaughterFind(string goodId)
        {
            var list=_SlaughterRepositoryrepository.GetAll().FirstOrDefault(p=>p.GoodId == goodId);
            return list;
        }
        /// <summary>
        /// 添加屠宰企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public int AddSlaughterEnterprise(T_SlaughterEnterprise enterprise)
        {
            UpdateGoodPerfect(enterprise.GoodId);
            _SlaughterEnterpriseRepositoryrepository.Add(enterprise);
            return SaveChange();
        }

        /// <summary>
        /// 修改屠宰企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        public int UpdateSlaughterEnterprise(T_SlaughterEnterprise enterprise)
        {
            _SlaughterEnterpriseRepositoryrepository.Update(enterprise);
            return SaveChange();
        }
        /// <summary>
        /// 反填屠宰企业
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_SlaughterEnterprise GetSlaughterEnterpriseFind(string goodId)
        {
            var list=_SlaughterEnterpriseRepositoryrepository.GetAll().FirstOrDefault(_=>_.GoodId == goodId);
            return list;
        }
        #endregion
        #region 加工
        /// <summary>
        /// 添加加工
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public int AddProcess(T_Process process)
        {
            UpdateGoodPerfect(process.GoodId);
            _ProcessRepositoryrepository.Add(process);
            return SaveChange();
        }
        /// <summary>
        /// 修改加工信息
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public int UpdateProcess(T_Process process)
        {
            _ProcessRepositoryrepository.Update(process);
            return SaveChange();
        }
        /// <summary>
        /// 反填加工信息
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_Process GetProcessFind(string goodId)
        {
            var list=_ProcessRepositoryrepository.GetAll().FirstOrDefault(p=>p.GoodId== goodId);
            return list;
        }
        /// <summary>
        /// 添加加工企业
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public int AddProcessEnterprise(T_ProcessEnterprise process)
        {
            UpdateGoodPerfect(process.GoodId);
            _ProcessEnterpriseRepositoryrepository.Add(process);
            return SaveChange();
        }
        /// <summary>
        /// 修改加工企业
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public int UpdateProcessEnterprise(T_ProcessEnterprise process)
        {
            _ProcessEnterpriseRepositoryrepository.Update(process);
            return SaveChange();
        }
        /// <summary>
        /// 反填加工企业
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_ProcessEnterprise GetProcessEnterpriseFind(string goodId)
        {
            var list=_ProcessEnterpriseRepositoryrepository.GetAll().FirstOrDefault(m=>m.GoodId== goodId);
            return list;
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public int AddSupplier(T_Supplier supplier)
        {
            UpdateGoodPerfect(supplier.GoodId);
            _SupplierRepositoryrepository.Add(supplier);
            return SaveChange();
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public int UpdateSupplier(T_Supplier supplier)
        {
            _SupplierRepositoryrepository.Update(supplier);
            return SaveChange();
        }
        /// <summary>
        /// 反填供应商
        /// </summary>
        /// <param name="goodId"></param>
        /// <returns></returns>
        public T_Supplier GetSupplierFind(string goodId)
        {
            var list = _SupplierRepositoryrepository.GetAll().FirstOrDefault(c => c.GoodId == goodId);
            return list;
        }
        #endregion
        private void UpdateGoodPerfect(string goodId)
        {
            var list = _GoodRepositoryrepository.GetAll().FirstOrDefault(p => p.GoodId == goodId);
            list.GoodPerfect +=1;
            _GoodRepositoryrepository.Update(list);
            SaveChange();
        }


    }
}
